const Router = require('@koa/router')
const router = new Router()
const { userLogin, userRegister, findUser,userUpdate,findUserById } = require('../controller/index.js')
const { sign, verify, refreshVerify } = require('../utils/jwt')
const { escape } = require('../utils/security')

router.prefix('/user')
router.post('/login', async (ctx) => {
    //1.获取请求体中的账号和密码
    //post请求携带的参数都在请求体中
    let { username, password } = ctx.request.body
   //注册时已经转译
   username = escape(username)
   password = escape(password)
    //检验账号密码是否合法
    //去数据库查询
    const res = await userLogin(username, password)
    try {
        if (res.length > 0) {
            let data = {
                id: res[0].id,
                username: res[0].username,
                nickname: res[0].nickname,
                createTime: res[0].create_Time,
            }
            const access_token = sign(data, '1h')
            const refresh_token = sign(data, '7d')

            ctx.body = {
                code: '1',
                msg: '登录成功',
                data: data,
                access_token: access_token,
                refresh_token: refresh_token
            }
        }
        else {   //逻辑性错误
            ctx.body = {
                code: '0',
                msg: '账号或密码错误',
                data: {}
            }
        }
    }
    catch (error) { //程序性错误
        ctx.body = {
            code: '-1',
            msg: '服务器异常',
            data: error
        }
    }
})
//刷新token
router.post('/refresh', (ctx) => {
    const { refresh_token } = ctx.request.body
    //校验refresh_token是否有效
    const decoded = refreshVerify(refresh_token)
    if (decoded.id) {
        //刷新token
        const data = {
            id: decoded.id,
            username: decoded.username,
            nickname: decoded.nickname,
            createTime: decoded.createTime,
        }
        const access_token = sign(data, '1h')
        const refresh_token = sign(data, '7d')
        ctx.body = {
            code: '1',
            msg: '刷新成功',
            data: {
                access_token,
                refresh_token
            }
        }
    } else {   //长 token 都过期了
        ctx.status = 416
        ctx.body = {
            code: '0',
            msg: '登录失败',
        }
    }
})

//注册接口
router.post('/register', async (ctx) => {
    let { username, password, nickname } = ctx.request.body
    //转译标签,防止sql注入
    username = escape(username)
    password = escape(password)
    nickname = escape(nickname)
    if (!username || !password || !nickname) {
        ctx.body = {
            code: '0',
            msg: '账号、密码、昵称不能为空',
            data: {}
        }
    }
    //检验账号是否存在
    const res = await findUser(username)
    try {
        if (res.length > 0) {
            ctx.body = {
                code: '0',
                msg: '账号已存在',
                data: {}
            }
        }
        if (res.length === 0) {
            //注册账号
            const res = await userRegister(username, password, nickname, { create_time: Date.now() })
            console.log(res)
            if (res.affectedRows > 0) {
                ctx.body = {
                    code: '1',
                    msg: '注册成功',
                    data: {}
                }
            }
            else {
                ctx.body = {
                    code: '0',
                    msg: '注册失败',
                    data: {}
                }
            }
        }
    }
    catch (error) {
        ctx.body = {
            code: '-1',
            msg: '服务器异常',
            data: error
        }
    }
})
module.exports = router